<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

/**
 *  Class to assist select statement
 */

/**
 *  Class to assist select statement
 */

class JFX_Db_Select
{
    /**
     * Whether to use a limit clause
     * @var bool $useLimit
     */
    private $useLimit = false;

    /**
     * Parameters of the limit clause
     * @var array $limit
     */
    private $limit = array('num'=>null, 'start'=>0);

    /**
     * Specify the tablename and columns to select
     * @var array $from
     */
    private $from = array('table'=>'', 'what'=>'');

    /**
     * Whether to use the where clause
     * @var bool $useWhere
     */
    private $useWhere = false;

    /**
     * Details of the where statement
     * @var array $where
     */
    private $where = array();

    /**
     * Whether to use the order clause
     * @var bool $useOrder
     */
    private $useOrder = false;

    /**
     * Details of the order clause
     * @var array $order
     */
    private $order = array();

    /**
     * Details of join statements
     * @var array $joins
     */
    private $joins = array();

    /**
     * Instantiation constructor
     */
    public function __construct()
    {

    }

    /**
     * Return the statement as a query string
     * @return string $query
     */
    public function __toString()
    {
        $joins = array();
        foreach($this->joins as $k=>$v){
          $joins[] = ' JOIN '.$v['tablename'].' ON '.$v['on'];
        }
        $q = "SELECT {$this->from['what']} FROM {$this->from['table']} ";
        $q .= implode(' ', $joins);
        if($this->useWhere) $q .= ' WHERE '.implode(' AND ', $this->where).' ';
        if($this->useOrder) $q .= ' ORDER BY '.implode(' , ', $this->order);
        if($this->useLimit) $q .= " LIMIT {$this->limit['num']} OFFSET {$this->limit['start']}";
        return $q;
    }

    /**
     * Join a table
     * @param string $tablename
     * @param string $on
     */
    public function join($tablename, $on){
      $this->joins[] = array('tablename' => $tablename, 'on' => $on);
    }

    /**
     * Get the from details as array
     * @return array $from
     */
    public function getFromAsArray(){
      return $this->from;
    }

    /**
     * Specify which columns to select from which table
     * @param string $tablename
     * @param string $what
     * @return object Antz_Db_Select
     */
    public function from($tablename, $what='')
    {
        $this->from['table'] = $tablename;
        if($what!='') $this->what($what);
        return $this;
    }

    /**
     * Specify which columns to select
     * @param array $what
     * @return object Antz_Db_Select
     */
    public function what($what){
      $this->from['what'] = $what;
      return $this;
    }

    /**
     * Add a where clause
     * @param string $colname
     * @param string $value
     * @param string $match
     * @return object Antz_Db_Select
     */
    public function where($colname, $value, $match='=')
    {
        $this->useWhere = true;
        $this->where[] = $colname.' '.$match." '".mysql_real_escape_string($value)."'";
        return $this;
    }

    /**
     * Specify limit parameters
     * @param int $num
     * @param int $start
     * @return object Antz_Db_Select
     */
    public function limit($num, $start=0)
    {
        $this->useLimit = true;
        $this->limit['num'] = $num;
        $this->limit['start'] = $start;
        return $this;
    }

    /**
     * Specify the order by clause
     * @param string $orderColumn
     * @return object Antz_Db_Select
     */
    public function order($order)
    {
        $this->useOrder = true;
        $this->order[] = $order;
        return $this;
    }

    /**
     * Sets the limit and count by page number.
     *
     * @param int $page Limit results to this page number.
     * @param int $rowCount Use this many rows per page.
     * @return object Antz_Db_Select
     */
    public function limitPage($page, $rowCount)
    {
        $page     = ($page > 0)     ? $page     : 1;
        $rowCount = ($rowCount > 0) ? $rowCount : 1;
        $this->limit($rowCount * ($page-1), $rowCount);
        return $this;
        // Zend fw
        $this->_parts['limitCount']  = (int) $rowCount;
        $this->_parts['limitOffset'] = (int) $rowCount * ($page - 1);
        return $this;
    }

}
